import { fileURLToPath, URL } from 'node:url'
import { defineConfig, ServerOptions } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

const server: ServerOptions = {
    port: 8082,
    host: '0.0.0.0',
    proxy: {
        '/api': {
            target: 'http://localhost:5000',
            changeOrigin: true,
            rewrite: (path: string) => path.replace(/^\/api/, '/api'),
        },
    },
}

// https://vitejs.dev/config/
export default defineConfig({
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url)),
            '@/api': fileURLToPath(new URL('./src/api', import.meta.url)),
            '@/store': fileURLToPath(new URL('./src/store', import.meta.url)),
            '@/components': fileURLToPath(new URL('./src/components', import.meta.url)),
        },
    },
    plugins: [
        vue(),
        AutoImport({
            resolvers: [ElementPlusResolver()],
        }),
        Components({
            resolvers: [ElementPlusResolver()],
        }),
    ],
    css: {
        preprocessorOptions: {
            less: {
                javascriptEnabled: true,
                modifyVars: {
                    hack: 'true; @import "./src/assets/css/global.less"',
                },
                // additionalData: `@import ${fileURLToPath(new URL('./src/assets/css/global.less'))}`,
            },
        },
    },
    server: server,
})
